#include <QPainter>

#include "ColorWidget.h"

ColorWidget::ColorWidget( QWidget *parent ) : QFrame( parent )
{
	setFrameStyle( QFrame::Panel | QFrame::Sunken );
	setLineWidth( 2 );
	
	m_color = Qt::black;
}

void ColorWidget::paintEvent( QPaintEvent *ev )
{
	ev = ev; // Just to avoid warnings of unused var
	QPainter p( this );
	QRect contents = contentsRect();
	contents.adjust( 0, 0 ,-lineWidth(), -lineWidth() );

	drawFrame( &p );
	p.setPen( m_color );
	p.setBrush( m_color );
	p.drawRect( contents );
}

void ColorWidget::setColor( const QColor &color )
{
	int r,g,b;
	int m_r, m_g, m_b;
	
	color.getRgb( &r, &g, &b );
	m_color.getRgb( &m_r, &m_g, &m_b );
	
	m_color = color;
	
	if( r != m_r || g != m_g || b != m_b )
	{
		emit( colorChanged( m_color ) );
		repaint();
	}
}

void ColorWidget::setR( int v )
{
	int r, g, b;
	
	m_color.getRgb( &r, &g, &b );
	r = v;
	
	setColor( QColor::fromRgb( r, g, b ) );
}

void ColorWidget::setG( int v )
{
	int r, g, b;
	
	m_color.getRgb( &r, &g, &b );
	g = v;
	
	setColor( QColor::fromRgb( r, g, b ) );
}

void ColorWidget::setB( int v )
{
	int r, g, b;
	
	m_color.getRgb( &r, &g, &b );
	b = v;
	
	setColor( QColor::fromRgb( r, g, b ) );
}

